package com.ecoolex.union.pay.retailer.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.union.pay.retailer.core.dao.UnionPayPopularMerchantProductDAO;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchant;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchantProduct;
import com.ecoolex.union.pay.retailer.entity.UnionPayPopularMerchantProductExample;
import com.ecoolex.union.pay.retailer.pojo.AddPopularMerchantProductDTO;
import com.ecoolex.union.pay.retailer.pojo.UnionPayPopularMerchantProductDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UnionPayPopularMerchantProductManager {
    @Autowired
    private UnionPayPopularMerchantProductDAO dao;


    public List<UnionPayPopularMerchantProductDTO> getPopularProducts(String merchantId) {
        UnionPayPopularMerchantProductExample productExample = new UnionPayPopularMerchantProductExample();
        UnionPayPopularMerchantProductExample.Criteria productCriteria = productExample.createCriteria();
        productCriteria.andDeletedEqualTo(UnionPayPopularMerchantProduct.NOT_DELETED)
                .andMerchantIdEqualTo(merchantId);
        List<UnionPayPopularMerchantProduct> products = dao.selectByExample(productExample);
        List<UnionPayPopularMerchantProductDTO> productDTOs = products.stream().map(product -> warp(product)).collect(Collectors.toList());
        return productDTOs;
    }

    private UnionPayPopularMerchantProductDTO warp(UnionPayPopularMerchantProduct product) {
        UnionPayPopularMerchantProductDTO dto = new UnionPayPopularMerchantProductDTO();
        BeanUtils.copyProperties(product, dto);
        dto.setCurrencyCode(UnionPayPopularCurrencyCodeManager.translateCurrencyCode(dto.getCurrencyCode()));
        return dto;
    }

    public Pagination<UnionPayPopularMerchantProduct> queryMerchantProduct(String merchantId, Page page) {
        UnionPayPopularMerchantProductExample example = new UnionPayPopularMerchantProductExample();
        UnionPayPopularMerchantProductExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(UnionPayPopularMerchantProduct.NOT_DELETED)
        .andMerchantIdEqualTo(merchantId);
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());
        return Pagination.create(page,
                dao.countByExample(example),
                () -> dao.selectByExample(example));
    }

    public UnionPayPopularMerchantProduct addMerchantProduct(AddPopularMerchantProductDTO record, String userName){
        record.setDeleted(UnionPayPopularMerchantProduct.NOT_DELETED);
        record.setCreateUser(userName);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(userName);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }
    public UnionPayPopularMerchantProduct get(Integer id){
        UnionPayPopularMerchantProductExample example = new UnionPayPopularMerchantProductExample();
        UnionPayPopularMerchantProductExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id)
                .andDeletedEqualTo(UnionPayPopularMerchant.NOT_DELETED);
        return dao.selectOneByExample(example);
    }

    public UnionPayPopularMerchantProduct editMerchantProduct(UnionPayPopularMerchantProduct record, String userName){
        record.setUpdateUser(userName);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, UnionPayPopularMerchantProduct.Column.excludes(
                UnionPayPopularMerchantProduct.Column.deleted,
                UnionPayPopularMerchantProduct.Column.createUser,
                UnionPayPopularMerchantProduct.Column.createTime));
        return record;
    }

    public List<UnionPayPopularMerchantProduct> getPopularProduct(String merchantId) {
        UnionPayPopularMerchantProductExample productExample = new UnionPayPopularMerchantProductExample();
        UnionPayPopularMerchantProductExample.Criteria productCriteria = productExample.createCriteria();
        productCriteria.andDeletedEqualTo(UnionPayPopularMerchantProduct.NOT_DELETED)
                .andMerchantIdEqualTo(merchantId);
        List<UnionPayPopularMerchantProduct> products = dao.selectByExample(productExample);
        return products;
    }

    public void delete(UnionPayPopularMerchantProduct product, String userName) {
        product.setDeleted(UnionPayPopularMerchantProduct.IS_DELETED);
        product.setUpdateUser(userName);
        product.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(product, UnionPayPopularMerchantProduct.Column.deleted, UnionPayPopularMerchantProduct.Column.updateUser, UnionPayPopularMerchantProduct.Column.updateTime);
    }
}